package com.linh.trade.gui;

import static com.linh.trade.Constants.AMOUNT_FORMAT;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import com.linh.trade.Trade;
import com.linh.trade.TraderActionListener;

public class Console extends JPanel implements TraderActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Line> lines = new ArrayList<Console.Line>();

	public void onGoingLong(Trade trade, double budget) {
		String text = (trade.getOpenLine() + " ("
				+ AMOUNT_FORMAT.format(budget) + ")");
		Color color = Color.green;
		addLine(new Line(text, color));
		repaint();
	}

	public void onGoingShort(Trade trade, double budget) {
		String text = (trade.getOpenLine() + " ("
				+ AMOUNT_FORMAT.format(budget) + ")");
		Color color = Color.yellow;
		addLine(new Line(text, color));
		repaint();
	}

	public void onPositionClosed(Trade trade, double budget) {
		String text = (trade.getCloseLine() + " ("
				+ AMOUNT_FORMAT.format(budget) + ")");
		Color color = Color.white;
		addLine(new Line(text, color));
		repaint();
	}
	
	public void clear(){
		lines.clear();
	}

	void addLine(Line line) {
		if (lines.size() > 5) {
			lines.remove(0);
		}
		lines.add(line);
	}

	@Override
	public void paint(Graphics g) {

		super.paint(g);
		g.setColor(Color.black);
		g.fillRect(0, 0, getWidth(), getHeight());
		int x = 20;
		int y = getHeight() - 10;
		for (int i=0;i<lines.size();i++) {
			Line line=lines.get(i);
			g.setColor(line.color);
			g.drawString(line.text, x, y);
			y = y - 15;
		}

	}

	class Line {
		String text;
		Color color;

		public Line(String text, Color color) {
			super();
			this.text = text;
			this.color = color;
		}

	}

}
